#include "data_global.h"
#include "mpu6050_driver.h"

extern struct env_info_client_addr sm_all_env_info;
extern pthread_mutex_t mutex_transfer;
extern pthread_mutex_t mutex_zigbee;
extern int fd_zigbee;

extern int set_com_config(int fd, int baud_rate, int data_bits, char parity, int stop_bits);
extern int open_port(char *com_port);
extern void USB_UART_Config(char* path, int baud_rate);

int file_fill_mpu6050_buf(int fd, int home_id);
int file_fill_adc_buf(int fd, int home_id);
int file_fill_zigbee_buf(int home_id);

struct zbuf_t zbuf;

// 接收ZigBee的数据和采集的A9平台的传感器数据
void *pthread_transfer(void *arg)
{
    printf("pthread transfer\n");
    int fd_mpu6050 = -1;
    int fd_adc = -1;
    if((fd_mpu6050 = open(MPU6050_DEV, O_RDONLY)) == -1)
    {
        perror("open");
        exit(1);
    }
    if((fd_adc = open(ADC_DEV, O_RDONLY)) == -1)
    {
        perror("open");
        exit(1);
    }
    pthread_mutex_lock(&mutex_transfer);
    ((struct makeru_a9_info*)sm_all_env_info.monitor_no[HOME_ID].a9_info.buffer)->head[0] = 'm';
    ((struct makeru_a9_info*)sm_all_env_info.monitor_no[HOME_ID].a9_info.buffer)->head[1] = 's';
    ((struct makeru_a9_info*)sm_all_env_info.monitor_no[HOME_ID].a9_info.buffer)->head[2] = 'm';
    ((struct makeru_a9_info*)sm_all_env_info.monitor_no[HOME_ID].a9_info.buffer)->type = 'a';
    ((struct makeru_zigbee_info*)sm_all_env_info.monitor_no[HOME_ID].zigbee_info.buffer)->head[0] = 'm';
    ((struct makeru_zigbee_info*)sm_all_env_info.monitor_no[HOME_ID].zigbee_info.buffer)->head[1] = 's';
    ((struct makeru_zigbee_info*)sm_all_env_info.monitor_no[HOME_ID].zigbee_info.buffer)->head[2] = 'm';
    ((struct makeru_zigbee_info*)sm_all_env_info.monitor_no[HOME_ID].zigbee_info.buffer)->type = 'z';
    pthread_mutex_unlock(&mutex_transfer);
    while(1)
    {
        sleep(1);
        file_fill_mpu6050_buf(fd_mpu6050, HOME_ID);
        file_fill_adc_buf(fd_adc, HOME_ID);
        if(fd_zigbee != -1) file_fill_zigbee_buf(HOME_ID);
    }
    close(fd_mpu6050);
    close(fd_adc);
}

int file_fill_mpu6050_buf(int fd, int home_id)
{
    union mpu6050_data data;
    pthread_mutex_lock(&mutex_transfer);
    ioctl(fd, GET_ACCEL, &data);
    ((struct makeru_a9_info*)sm_all_env_info.monitor_no[home_id].a9_info.buffer)->accx = (float)data.accel.x/16384.0;
    ((struct makeru_a9_info*)sm_all_env_info.monitor_no[home_id].a9_info.buffer)->accy = (float)data.accel.y/16384.0;
    ((struct makeru_a9_info*)sm_all_env_info.monitor_no[home_id].a9_info.buffer)->accz = (float)data.accel.z/16384.0;

    ioctl(fd, GET_GYRO, &data);
    ((struct makeru_a9_info*)sm_all_env_info.monitor_no[home_id].a9_info.buffer)->gyrox = (float)data.gyro.x/16.4;
    ((struct makeru_a9_info*)sm_all_env_info.monitor_no[home_id].a9_info.buffer)->gyroy = (float)data.gyro.y/16.4;
    ((struct makeru_a9_info*)sm_all_env_info.monitor_no[home_id].a9_info.buffer)->gyroz = (float)data.gyro.z/16.4;
    
    // ioctl(fd, GET_TEMP, &data);
    // ((struct makeru_a9_info*)sm_all_env_info.monitor_no[home_id].a9_info.buffer)->adc = 36.53 + (float)evt.value/340.0;
    pthread_mutex_unlock(&mutex_transfer);
    return 0;
}

int file_fill_adc_buf(int fd, int home_id)
{
    int buf_adc = 0;
    read(fd, &buf_adc, sizeof(int));
    pthread_mutex_lock(&mutex_transfer);
    ((struct makeru_a9_info*)sm_all_env_info.monitor_no[home_id].a9_info.buffer)->adc = (1.8)*buf_adc/4096.0;
    pthread_mutex_unlock(&mutex_transfer);
    return 0;
}

int file_fill_zigbee_buf(int home_id)
{
    int ret = 0;
    
    pthread_mutex_lock(&mutex_zigbee);
    ret = read(fd_zigbee, &zbuf, sizeof(zbuf));
    // printf("ret = %d\n", ret);
    // printf("zbuffan = %d, zbuftem = %f, zbufhum = %f\n", zbuf.fan, zbuf.tem/100.0, zbuf.hum/100.0);

    if(ret > 0)
    {
        pthread_mutex_lock(&mutex_transfer);
        ((struct makeru_zigbee_info*)sm_all_env_info.monitor_no[home_id].zigbee_info.buffer)->temperature = zbuf.tem/100.0;
        ((struct makeru_zigbee_info*)sm_all_env_info.monitor_no[home_id].zigbee_info.buffer)->humidity = zbuf.hum/100.0;
        pthread_mutex_unlock(&mutex_transfer);
    }

    pthread_mutex_unlock(&mutex_zigbee);
    return 0;
}
